package com.admin.scroller.photoorvideo.holders;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.admin.scroller.photoorvideo.PerivewActivity;
import com.admin.scroller.photoorvideo.PhotoChoseActivity;
import com.admin.scroller.photoorvideo.R;
import com.admin.scroller.photoorvideo.beans.LocalPhoto;
import com.admin.scroller.photoorvideo.utils.FirstBitmapTask;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;

import java.io.File;

public class ChildGridHolder extends RecyclerView.ViewHolder {

    private View contentView;
    private ImageView imageView;
    private ImageView selected;
    private LinearLayout linearDuraction;
    private TextView textDuration;
    private static RequestManager manager;
    private OnImageItemClick imageItemClick;
    private static Context mcontext;

    public static ChildGridHolder getHolder(Context context, ViewGroup group, int type){
        View view=LayoutInflater.from(context).inflate(R.layout.layout_item_image,group,false);
        mcontext=context;
        manager=Glide.with(context);
        return new ChildGridHolder(view);
    }

    public void setImageItemClick(OnImageItemClick imageItemClick){
        this.imageItemClick=imageItemClick;
    }

    private ChildGridHolder(View itemView) {
        super(itemView);
        contentView=itemView;
        imageView=contentView.findViewById(R.id.iv_item_image);
        selected=contentView.findViewById(R.id.iv_selected);
        linearDuraction=contentView.findViewById(R.id.linear_duration);
        textDuration=contentView.findViewById(R.id.tv_duration);
    }

    public void initData(final LocalPhoto localPhoto, final int position, final int persent){
        String path;
        if(localPhoto.isVideo()){
            linearDuraction.setVisibility(View.VISIBLE);
            textDuration.setText(localPhoto.getVideoDecation());
            path=localPhoto.getVideoUrl();
        }else {
            path=localPhoto.getPath();
            linearDuraction.setVisibility(View.GONE);

        }
        if (!TextUtils.isEmpty(path)) {
            File file = new File(path);
            if (file.exists()) {
                manager.load(file).into(imageView);
            }
        }
        if(PhotoChoseActivity.viewModel.checkHasChosed(localPhoto.getPath(),localPhoto.getVideoUrl())){
            setSelectedImage(true);
        }else {
            setSelectedImage(localPhoto.isSelected());
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到预览
                String url;
                if(localPhoto.isVideo()) {
                    url=localPhoto.getVideoUrl();
                }else {
                    url=localPhoto.getPath();
                }
                Intent intent = new Intent(mcontext, PerivewActivity.class);
                intent.putExtra(PerivewActivity.FILE_PATH, url);
                intent.putExtra(PerivewActivity.TYPE,1);
                intent.putExtra(PerivewActivity.INDEX,position);
                intent.putExtra(PerivewActivity.INDEX_PARENT,persent);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    mcontext.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation((PhotoChoseActivity)mcontext, imageView, mcontext.getString(R.string.activity_show)).toBundle());
                }else {
                    mcontext.startActivity(intent);
                }
            }
        });

        selected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(imageItemClick!=null){
                    imageItemClick.onItemSelected(persent,position);
                }
            }
        });
    }

    private void setSelectedImage(boolean s){
        if(s){
            selected.setImageResource(R.drawable.mis_btn_selected);
        }else {
            selected.setImageResource(R.drawable.mis_btn_unselected);
        }
    }

    public interface OnImageItemClick{
        void onItemSelected(int persent,int position);
        void onItemClick(int persent,int position,LocalPhoto localPhoto);
    }

}
